
package jp.moisture.othello.player;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import jp.moisture.othello.board.Cell;
import jp.moisture.othello.board.Stone;
import jp.moisture.othello.player.search.CandidateCell;

public class SimplePlayer extends Player {

    public SimplePlayer(String name) {
        super(name);
    }

    public Cell next() {
        List<Cell> cells = mBoard.findPutableCells(mColor);
        if (cells.isEmpty()) {
            return null;
        } else if (cells.size() == 1) {
            return cells.get(0);
        }
        
        List<CandidateCell> candidateCells = new ArrayList<>();
        for (Cell cell : cells) {
            CandidateCell candidateCell = new CandidateCell(mColor, mBoard, cell);
            if (!candidateCell.isWideMarginWon(Stone.otherColor(mColor))) {
                candidateCells.add(candidateCell);
            }
        }
        if (candidateCells.isEmpty()) {
            return cells.get(0);
        }
        Collections.sort(candidateCells);
        
        Cell candidateCell = candidateCells.get(0).mCell;
        return mBoard.getCell(candidateCell.getX(), candidateCell.getY());
    }

}
